﻿$(function () {

    if ($.browser.msie) {
        PrepareUploadify();
    }
    else {
        tinyMCEPopup.onInit.add(function (ed) {
            PrepareUploadify();
        });
    }

    
});

function PrepareAjaxUpload() {
    var button = $('#btnFile'), interval;

    new AjaxUpload(button, {
        action: '/Services/FileHandler.ashx',
        name: 'Filedata',
        onSubmit: function (file, ext) {
            // change button text, when user selects file
            button.text('Uploading');

            // If you want to allow uploading only 1 file at time,
            // you can disable upload button
            this.disable();

            // Uploding -> Uploading. -> Uploading...
            interval = window.setInterval(function () {
                var text = button.text();
                if (text.length < 13) {
                    button.text(text + '.');
                } else {
                    button.text('Uploading');
                }
            }, 200);
        },
        onComplete: function (file, response) {
            button.text('Upload');

            window.clearInterval(interval);

            // enable upload button
            this.enable();

            // add file to the list
            $('<li></li>').appendTo(button.parent()).text(file);
        }
    });
}

function PrepareUploadify() {
    $('#fileInput1').uploadify({
        uploader: '/Scripts/uploadify/uploadify.swf',
        script: '/Services/FileHandler.ashx',
        scriptData: {},
        cancelImg: '/Content/images/icons/cancel.png',
        auto: true,
        multi: false,
        'fileDesc': '图片格式 (*.jpg;*.png;*.gif;*.bmp;*.jpeg)',
        'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
        'queueSizeLimit': 20,
        'sizeLimit': 2000000,
        'simUploadLimit': 1,
        width: 91,
        height: 27,
        buttonImg: "/Content/images/insert-image.png",
        onComplete: function (event, queueID, fileObj, response, data) {
            var tmp = eval('(' + response + ')');
            //var html = "<a rel=\"" + tmp.ImagePath + "\" class=\"postimg\"><img src=\"/Content/Uploads/" + tmp.ThumbPath + "\" alt=\"\" border=\"0\" /></a>";
            var src = "/Content/Uploads/" + tmp.ThumbPath;
            $('#src').val(src);
            $('#alt').val(" ");
            
            var html = '<img src="' + src + '" class="postimg" alt="" border="0" />';
            //$('#imgPreview').html(html);
            //$('#imgPreview img').css('max-width', 300);
            
            ImageDialog.showPreviewImage(src, 1);
        }
    });
}


